package com.mdxx.xczd.ui.model;

import android.text.TextUtils;
import com.google.gson.Gson;
import com.mdxx.xczd.api.ApiManage;
import com.mdxx.xczd.app.App;
import com.mdxx.xczd.base.baserx.RxSchedulers;
import com.mdxx.xczd.bean.Alarm;
import com.mdxx.xczd.bean.AlarmBeanResult;
import com.mdxx.xczd.bean.CancelAlarmResult;
import com.mdxx.xczd.bean.FileBean;
import com.mdxx.xczd.bean.FinishedPoint;
import com.mdxx.xczd.bean.SubmitFileResult;
import com.mdxx.xczd.bean.TaskBeanResult;
import com.mdxx.xczd.bean.VideoBean;
import com.mdxx.xczd.ui.contract.TaskContract;
import com.orhanobut.logger.Logger;

import java.io.File;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;

/**
 * Created by MD-02 on 2017/1/17.
 */

public class TaskModel implements TaskContract.Model {

    @Override
    public Observable<TaskBeanResult> getTaskList() {
        return ApiManage.getInstence().getApiService().getTaskList(App.userBean.empNum)
                .compose(RxSchedulers.<TaskBeanResult>io_main());
    }

    @Override
    public Observable<SubmitFileResult> submitFile(FileBean filebean) {

        RequestBody requestBody ;
        MediaType mediaType = MediaType.parse("image/png");
        MultipartBody.Builder builder = new MultipartBody.Builder();
        if(TextUtils.equals("video", filebean.getType())){
            for (int i = 0; i < filebean.getPaths().length; i++) {
                File files = new File(filebean.getPaths()[i]);
                builder = builder.addFormDataPart("file", files.getName(), RequestBody.create(mediaType, files));
            }
            if(filebean.getAlarmNum() != null && !filebean.getAlarmNum().equals("")){
                builder = builder.addFormDataPart("alarmNum", filebean.getAlarmNum());
            }
        }else{
            File file = new File(filebean.getPath());
            builder = builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file));
        }
        builder = builder.addFormDataPart("empNum", filebean.getEmpNum());
        if(filebean.getTaskNum() != null && !filebean.getTaskNum().equals("")){
            builder = builder.addFormDataPart("taskNum", filebean.getTaskNum());
        }
        builder = builder.addFormDataPart("uploadTime", filebean.getUploadTime());
        builder = builder.addFormDataPart("type", "photo");
        builder = builder.addFormDataPart("lo",  filebean.getLo());
        builder = builder.addFormDataPart("la", filebean.getLa());
        requestBody = builder.build();
        return ApiManage.getInstence()
                .getApiService()
                .submitFile(requestBody)
                .compose(RxSchedulers.<SubmitFileResult>io_main());
    }

    @Override
    public Observable<SubmitFileResult> submitVideo(VideoBean videoBean) {
        RequestBody requestBody ;
        MediaType mediaType = MediaType.parse("image/png");
        MultipartBody.Builder builder = new MultipartBody.Builder();
        File file = new File(videoBean.getPath());
        builder = builder.addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file));
        builder = builder.addFormDataPart("empNum", videoBean.getEmpNum());
        builder = builder.addFormDataPart("type", "video");
        if(videoBean.getTaskNum() != null && !videoBean.getTaskNum().equals("")){
            builder = builder.addFormDataPart("taskNum", videoBean.getTaskNum());
        }
        if(videoBean.getAlarmNum() != null && !videoBean.getAlarmNum().equals("")){
            builder = builder.addFormDataPart("alarmNum", videoBean.getAlarmNum());
        }
        builder = builder.addFormDataPart("uploadTime", videoBean.getUploadTime());
        builder = builder.addFormDataPart("lo",  videoBean.getLo());
        builder = builder.addFormDataPart("la", videoBean.getLa());
        requestBody = builder.build();
        return ApiManage.getInstence()
                .getApiService()
                .submitVideo(requestBody)
                .compose(RxSchedulers.<SubmitFileResult>io_main());
    }

    @Override
    public Observable<AlarmBeanResult> sendAlarm(Alarm alarm) {
        String str=new Gson().toJson(alarm);
        Logger.d("str=="+str);
        return ApiManage.getInstence()
                .getApiService()
                .sendAlarm(str)
                .compose(RxSchedulers.<AlarmBeanResult>io_main());
    }

    @Override
    public Observable<TaskBeanResult> submitLocation(String coordinate) {
        return ApiManage.getInstence()
                .getApiService()
                .submitLocation(coordinate)
                .compose(RxSchedulers.<TaskBeanResult>io_main());
    }

    @Override
    public Observable<TaskBeanResult> submitFinishedPoint(FinishedPoint finishedPoint) {
        return ApiManage.getInstence()
                .getApiService()
                .submitFinishedPoint(finishedPoint.taskNum, finishedPoint.siteNum, finishedPoint.patrolTime)
                .compose(RxSchedulers.<TaskBeanResult>io_main());
    }

    @Override
    public Observable<CancelAlarmResult> cancelAlarm(String alarmNum) {

        return ApiManage.getInstence()
                .getApiService()
                .cancelAlarm(alarmNum)
                .compose(RxSchedulers.<CancelAlarmResult>io_main());
    }


}
